package pub.tbc.atps.template;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import pub.tbc.atps.task.ParamType;
import pub.tbc.atps.task.RestTask;
import pub.tbc.toolkit.CloseUtil;
import pub.tbc.toolkit.Objs;
import pub.tbc.toolkit.io.StreamUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 *
 * @author tbc on 2016/12/1 15:31:29.
 */
@Slf4j
public class RequestTemplate {
    private static final Object lock = new Object();
    private static RequestTemplate template = null;
    private HttpTemplate httpTemplate = new HttpTemplate();
    private EntityTemplate entityTemplate = new EntityTemplate();
    private RestGetHttpTemplate restGetTemplate = new RestGetHttpTemplate();

    private String pathParamUrl(RestTask task) {
        Map<String, Object> param = task.getParam();
        if (Objs.isEmpty(param))
            return task.getUrl();
        StringBuilder sb = new StringBuilder(task.getUrl());
        param.forEach((k, v) -> sb.append("/").append(v));
        return sb.toString();
    }

    private String httpParamUrl(RestTask task) {

        return null;
    }

    private void httpRequest() {

    }

    private void entityRequest() {

    }

    private String restRequest(RestTask task) {
        String url = pathParamUrl(task);
        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();// 默认的配置
        try {
            httpclient.start();
            HttpGet request = new HttpGet(url);
            Future<HttpResponse> future = httpclient.execute(request, null);
            HttpResponse response = future.get();// 获取结果
            log.debug("Response: " + response.getStatusLine());
            InputStream in = null;
            try {
                in = response.getEntity().getContent();
            } catch (IOException e) {
                e.printStackTrace();
            }
            assert in != null;
            return StreamUtil.read(in);
        } catch (InterruptedException e) {
            log.error("请求错误， InterruptedException： {}", e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        } catch (ExecutionException e) {
            log.error("请求错误， ExecutionException： {}", e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            CloseUtil.close(httpclient);
        }
    }


    public String request(RestTask task) {
        String strParamType = task.getParamType();
        if (Objs.isEmpty(strParamType))
            return null;
        ParamType paramType = ParamType.getParamType(strParamType);
        assert paramType != null;
        switch (paramType) {
            case HTTP_PARAM:
                httpRequest();
                break;
            case PATH_PARAM:
                return restRequest(task);
            case ENTITY_PARAM:
                entityRequest();
            default:
                throw new RuntimeException("不支持的参数类型");
        }
        return null;
    }

    public static RequestTemplate getInstance() {
        if (Objs.isEmpty(template)) {
            synchronized (lock) {
                if (Objs.isEmpty(template)) {
                    template = new RequestTemplate();
                }
            }
        }
        return template;
    }

}
